<template>
  <div>
    <!-- 默认单选框 -->
    <el-radio-group v-if="type === 'radio'" v-model="inputValue" :disabled="disabled" size="small">
      <el-radio v-for="(value, key) in dataOptions" :key="key" :label="key" name="radio">{{ value }}</el-radio>
    </el-radio-group>
    <!-- 按钮形式的单选框 -->
    <el-radio-group v-else v-model="inputValue" :disabled="disabled" size="small">
      <el-radio-button v-for="(value, key) in dataOptions" :key="key" :label="key" name="radioButton">{{
        value
      }}</el-radio-button>
    </el-radio-group>
  </div>
</template>

<script>
export default {
  name: "TRadio",
  model: {
    prop: "value",
    event: "change",
  },
  props: {
    value: {
      type: [String, Number],
      default: "",
    },
    /**
     * 选择器类型
     * radio 默认单选框
     * radioButton 按钮形式的单选框
     */
    type: {
      type: String,
      default: "radio",
      validator(val) {
        return ["radio", "radioButton"].includes(val);
      },
    },
    /**
     * 字典标识，默认 性别
     */
    dictCode: {
      type: String,
      default: "gender",
    },
    /**
     * 禁用状态
     */
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {};
  },
  computed: {
    // 选中值
    inputValue: {
      get() {
        return this.value;
      },
      set(val) {
        const label = val ? this.dataOptions[val] : "";
        this.$emit("change", val, label);
      },
    },
    // 选择器数据源
    dataOptions() {
      return this.$store.state.data.KV_MAP[this.dictCode] || {};
    },
  },
};
</script>

<style lang="scss" scoped></style>
